<?php
namespace app\api\controller\v2;
use app\api\controller\Common;

class Client extends Common {
	var $modelDb;
	public function _initialize() {
		parent::_initialize();
		$this->modelDb = model("Client");
	}
	public function add_only() {
		$ids = $this->modelDb->insert($this->postData);
		if ($ids === false) {
			return $this->app_error($this->modelDb->getError());
		}
		return $this->app_success();
	}
	// 录入线索
	public function add() {
		$this->postData['userid'] = $this->uinfo['puserid'];
		$this->postData['brokerid'] = $this->uinfo['userid'];
		$this->postData['dealerid'] = $this->uinfo['dealerid'];
		if (isset($this->postData['citycode'])) {
			$info = explode(',', $this->postData['citycode']);
			$this->postData['province_code'] = $info[0];
			$this->postData['city_code'] = $info[1];
			$this->postData['area_code'] = $info[2];
		}
		$id = $this->modelDb->save_datas($this->postData);
		if ($id === false) {
			return $this->app_error($this->modelDb->getError());
		}
		model('Statistics')->add_log($this->uinfo['userid'], 1, 1);
		return $this->app_success();
	}

	// 编辑线索
	public function update() {
		// $this->postData['userid'] = $this->uinfo['puserid'];
		// // $this->postData['brokerid'] = $this->uinfo['userid'];
		// // $this->postData['dealerid'] = $this->uinfo['dealerid'];
		// if (isset($this->postData['citycode'])) {
		// 	$info = explode(',', $this->postData['citycode']);
		// 	$this->postData['province_code'] = $info[0];
		// 	$this->postData['city_code'] = $info[1];
		// 	$this->postData['area_code'] = $info[2];
		// }
		$id = $this->modelDb->update_datas($this->postData);
		if ($id === false) {
			return $this->app_error($this->modelDb->getError());
		}
		model('Statistics')->add_log($this->uinfo['userid'], 1, 1);
		return $this->app_success();
	}

	//获取客户列表
	public function client_list() {
		list($total, $list) = $this->modelDb->list_datas($this->postData, $this->uinfo);
		if ($list === false) {
			return $this->modelDb->app_error($this->modelDb->getError());
		}
		$lists = [];
		foreach ($list as $v) {
			$v['realname'] = $v['realname'];
			$lists[] = $v;
		}
		return $this->app_success($lists, '', 200, ['pages' => ceil($total / 10)]);
	}
	public function get_clients() {
		$list = $this->modelDb->list_datas($this->postData, $this->uinfo['userid']);
		if ($list === false) {
			return $this->app_error($this->modelDb->getError());
		}
		return $this->app_success($list);
	}
	// 删除线索
	public function del_client() {
		// $password = model('AdminUser')->where('userid', $this->uinfo['userid'])->value('password');
		// if ($password != md5($this->postData['password'])) {
		// 	return $this->app_error('登录密码错误');
		// }
		$roleid = $this->uinfo['roleid'];
		if ($roleid == 9) {
			$ids = $this->modelDb->where('id', $this->postData['id'])->delete();
			return $this->app_success([], '删除成功');
		} else {
			return $this->app_error('没有删除权限');
		}

	}
	//转交客户
	public function client_deliver() {
		$info = db('Client')->where('id', $this->postData['clientid'])->find();
		$data = [
			'userid' => $this->postData['userid'],
			'name' => $info['name'],
			'product' => $info['product'],
			'phone' => $info['phone'],
			'email' => $info['email'],
			'qq' => $info['qq'],
			'city' => $info['city'],
			'getdate' => $info['getdate'],
			'home' => $info['home'],
			'source' => $info['source'],
			'skilled' => $info['skilled'],
			'status' => $info['status'],
			'remark' => $info['remark'],
			'endtime' => $info['endtime'],
			'typeid' => $info['typeid'],
			'delitime' => SYS_TIME,
			'price' => $info['price'],
		];
		$ids = $this->modelDb->save_data($data);
		if ($ids === false) {
			return $this->app_error($this->modelDb->getError());
		}
		$this->modelDb->where('id', $this->postData['clientid'])->setField('transfer', 1);
		return $this->app_success();
	}

	public function order_add() {
		$ids = model("OrderClient")->save_datas($this->postData, $this->uinfo['userid'], $this->uinfo['dealerid']);
		if ($ids === false) {
			return $this->app_error(model('OrderClient')->getError());
		}
		return $this->app_success();
	}
	public function order_update() {
		$ids = model("OrderClient")->update_datas($this->postData, $this->uinfo['userid']);
		if ($ids === false) {
			return $this->app_error(model('OrderClient')->getError());
		}
		return $this->app_success();
	}
	public function del_order() {
		$ids = model("OrderClient")->where('id', $this->postData['id'])->setField('isdel', 1);
		if ($ids === false) {
			return $this->app_error(model('OrderClient')->getError());
		}
		return $this->app_success();
	}
	// 成交的客户
	public function deal_client() {
		trace($this->postData);
		$orderlist = model('Client')->where('id', $this->postData['clientid'])->where('status', '2')->field('id, kind, price, product, carthumb, status, sucdate')->select();
		// if (isset($orderlist)) {
		// 	foreach ($orderlist as $key => $value) {
		// 		if ($key == 'cartypeid2') {
		// 			$id = $value;

		// 		}
		// 	}
		// }
		return $this->app_success($orderlist);
	}

	//奖励金列表
	public function bonus_list() {
		$page = isset($this->postData['page']) ? $this->postData['page'] : 1;
		$limit = 15;
		$list = model('MemberBouns')->where('userid', $this->uinfo['userid'])->order('id desc')->page($page, $limit)->select();
		if ($list === false) {
			return $this->app_error('获取失败');
		}
		$total = model('MemberBouns')->where('userid', $this->uinfo['userid'])->count();
		return $this->app_success($list, '', 200, ['pages' => ceil($total / $limit)]);
	}

	// 确认奖励金
	public function confirm_jianglj() {
		$clientid = $this->postData['clientid'];
		$status = model('MemberBouns')->where('userid', $this->uinfo['userid'])->where('clientid', $clientid)->value('status');
		if (!$status) {
			return $this->app_error('操作失败!');
		} elseif ($status == 2) {
			return $this->app_error('请不要重复确认！');
		}
		$ids = model('MemberBouns')->where('userid', $this->uinfo['userid'])->where('clientid', $clientid)->update(['status' => 2, 'audittime' => time()]);
		if ($ids === false) {
			return $this->app_error('操作失败，请重新提交');
		}
		return $this->app_success();
	}

	// 提交申诉
	public function add_compt() {
		if ($this->postData && isset($this->postData['acce'])) {
			$imgArr = $this->postData['acce'];
			$images = '';
			foreach ($imgArr as $value) {
				$images = $images . $value . ',';
			}
			$this->postData['images'] = $images;
			$ids = model('Complaint')->save_datas($this->postData, $this->uinfo);
			if ($ids === false) {
				return $this->app_error(model('Complaint')->getError());
			}
			return $this->app_success();
		} else {
			return $this->app_error('提交失败');
		}

	}

	//申诉列表
	public function shensu_list() {
		$page = isset($this->postData['page']) ? $this->postData['page'] : 1;
		$limit = 10;
		$list = model('Complaint')->where('userid', $this->uinfo['userid'])->order('id desc')->page($page, $limit)->select();
		if ($list === false) {
			return $this->app_error('获取失败');
		}
		$total = model('Complaint')->where('userid', $this->uinfo['userid'])->count();
		return $this->app_success(['list' => $list], '', 200, ['pages' => ceil($total / $limit)]);
	}

	//我的线索详情
	public function client_detail() {
		$info = $this->modelDb->where(['id' => $this->postData['clientid']])->field('id, brokerid, name, phone, typeid, status, getdate, kind, product, price, home, remark, marketer, marketerphone')->find();
		if (isset($info) && isset($info['brokerid'])) {
			$brokerinfo = model('AdminUser')->where('userid', $info['brokerid'])->field('realname, phone, avatar')->find();
			if (isset($brokerinfo)) {
				$info['brokername'] = $brokerinfo['realname'];
				$info['brokerphone'] = $brokerinfo['phone'];
				$info['brokeravatar'] = $brokerinfo['avatar'];
			}
		}

		// $status = -1;
		// if ($info['kind'] == 'gouche') {
		// 	$status = model('MemberBouns')->where('userid', $this->uinfo['userid'])->where('clientid', $this->postData['clientid'])->value('status');
		// }
		// $this->assign('status', !is_null($status) ? $status : -1);
		return $this->app_success($info);
	}

	public function client_status_list() {
		$status = config('CLIENT_STATUS');
		if (is_array($status) && isset($status)) {
			$str_temp = [];
			$count = 0;
			foreach ($status as $key => $value) {
				if ($key != 0) {
					$arr_temp = [
						'id' => $key,
						'value' => $value,
					];
					$str_temp[$count++] = $arr_temp;
				}
			}
			// $returnData = json_encode($str_temp);
			return $this->app_success($str_temp);
		} else {
			return $this->app_error();
		}
	}

	//数据统计
	public function client_data_statistics() {
		$datewhere = [
			'startdate' => date('Y-m-d', 0),
			'enddate' => date('Y-m-d', SYS_TIME),
		];
		if (isset($this->postData['startdate'])) {
			$startdate = $this->postData['startdate'];
			$datewhere['startdate'] = $startdate;
			//
			if (isset($this->postData['enddate'])) {
				$enddate = $this->postData['enddate'];
				$datewhere['enddate'] = $enddate;
			} else {
				$datewhere['enddate'] = date('Y-m-d', SYS_TIME);
			}
		}
		//
		$otherwhere = [];
		if (isset($this->postData['userid'])) {
			$otherwhere['brokerid'] = $this->postData['userid'];
		}
		$roleid = $this->uinfo['roleid'];
		if($roleid == '8') { // 销售总监
			$otherwhere['kind'] = 'gouche';
		} else if ($roleid == '10') { // 增值总监
			$otherwhere['kind'] = 'xubao';
		} else if ($roleid == '11') { // 售后总监
			$otherwhere['kind'] = 'baoyang';
		}
		$otherwhere['dealerid'] = $this->uinfo['dealerid'];
		trace($otherwhere);
		// 所有的经纪人下的线索 
		$all_clients = model('Client')->where($otherwhere)->where('getdate', 'between', [$datewhere['startdate'], $datewhere['enddate']])->field('userid, name, dealerid')->select();
		// 所有的经纪人下的已经成交的线索
		$suc_clients = model('Client')->where($otherwhere)->where('getdate', 'between', [$datewhere['startdate'], $datewhere['enddate']])->where('status', '2')->field('userid, name, dealerid')->select();
		$returndata = [
			'all_clients_num' => count($all_clients),
			'suc_clients_num' => count($suc_clients),
		];
		$returndata['all_clients'] = $all_clients;
		$returndata['suc_clients'] = $suc_clients;
		return $this->app_success($returndata);
	}

}